package com.zw.yzk.kabu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.model.SearchExhibitionModel;
import com.zw.yzk.kabu.model.SearchTopicModel;
import com.zw.yzk.kabu.view.activity.ExhibitionDetailsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhanwei on 2017/1/7.
 */

public class SearchTopicAdapter extends RecyclerView.Adapter<SearchTopicAdapter.Holder> {

    private List<SearchTopicModel> dataList;
    private Context context;

    public SearchTopicAdapter(List<SearchTopicModel> list) {
        this.dataList = new ArrayList<>();
        if (list != null) {
            dataList.addAll(list);
        }
    }

    public void add(List<SearchTopicModel> list) {
        if (list != null) {
            int startPosition = dataList.size();
            dataList.addAll(list);
            notifyItemRangeInserted(startPosition, list.size());
        }
    }

    public void refresh(List<SearchTopicModel> list) {
        this.dataList.clear();
        if (list != null) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context == null) {
            context = parent.getContext();
        }
        View rootView = LayoutInflater.from(context).inflate(R.layout.recycler_item_search_topic_list, parent, false);

        return new Holder(rootView);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final SearchTopicModel model = dataList.get(position);
        holder.title.setText(model.getTopicContent());
        Glide.with(context)
                .load(model.getImage())
                .centerCrop()
                .error(R.drawable.im_default)
                .placeholder(R.drawable.im_default)
                .into(holder.image);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onTopicItemClicked(model);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public static class Holder extends RecyclerView.ViewHolder {
        @Bind(R.id.image)
        ImageView image;
        @Bind(R.id.title)
        TextView title;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickedListener {
        void onTopicItemClicked(SearchTopicModel model);
    }

    private OnItemClickedListener listener;

    public void setOnItemClickedListener(OnItemClickedListener listener) {
        this.listener = listener;
    }
}
